import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * This class is TO Add Class To Course UI
 */
public class AddClassToCourseUI  extends JPanel{
	/**
	 * Choose course option
	 */
	private JComboBox courseBox = null;
	/**
	 * Choose class option
	 */
	private JComboBox classBox = null;
	/**
	 * button for add class to course 
	 */
	private JButton btnDefine = null;
	/**
	 * back button 
	 */
	public JButton btnBack = null;
	/**
	 * title for the current panel  
	 */
	private JLabel lbltitle = null;
	/**
	 * help button 
	 */
	public JButton btnHelp = null;
	/**
	 * course list 
	 */
	private ArrayList<String> lcourse = new ArrayList<String>();
	/**
	 * class list 
	 */
	private ArrayList<String> lclass = new ArrayList<String>();
	/**
	 *  the chosen course
	 */
	private int courseFlg = 0;
	/**
	 *  the chosen class
	 */
	private int classFlg;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	* Constructs 
	*/
	public AddClassToCourseUI() {
		super();
		initialize();
	}
	/**
	 *  initialize the gui window
	 */
	private void initialize() {
		curPanel = this;
		
		this.add(getCourseBox(), null);
		this.add(getClassBox(), null);
	

	
		
		JLabel lblCourse = new JLabel("Course:");
		lblCourse.setBounds(new Rectangle(116, 79, 95, 25)); 
		this.add(lblCourse);
		
		JLabel lblClass = new JLabel("Class:");
		lblClass.setBounds(new Rectangle(116, 115, 95, 25)); 
		this.add(lblClass);

		
		lbltitle = new JLabel("Add Class To Course");
		lbltitle.setBounds(171, 31, 195, 14);
		this.add(lbltitle);
		
		
		btnDefine = new JButton();
		btnDefine.setBounds(new Rectangle(113, 199, 195, 25));
		btnDefine.setText("Add Class To Course");
		/**
		 *  add class to course Implementation
		 */
		btnDefine.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String arr[] = new String[2];
				arr[0] = lcourse.get(courseFlg);
				arr[1] = lclass.get(classFlg);
				SQL s = new SQL(arr,39);
				s.accept();
				ArrayList<String> l = new ArrayList<String>();
				l=(ArrayList<String>)s.getMsg();
				
				
				if (l.size()>=1) // changed to <= instead of > //
				{
					System.out.println("choose teacher ");
					AddTeacherUI addTeacherUI = new AddTeacherUI(l,lclass.get(classFlg));
					setVisible(false);
					Global.main.setContentPane(addTeacherUI);
					/**
					 *  back button Implementation
					 */	
					addTeacherUI.btnBack.addActionListener(new ActionListener() {      
							public void actionPerformed(ActionEvent arg0) {
								Global.main.setContentPane(curPanel);
								curPanel.setVisible(true);
							}
						});
				}
			}
		});
			
		this.add(btnDefine);
	
		btnBack = new JButton("Back");
		btnBack.setBounds(161, 252, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		/**
		 *  help button Implementation
		 */
		btnHelp = new JButton("Help");
		btnHelp.setBounds(333, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose course type and click on 'Browse' button or ","on 'Define New Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);
		
		this.setVisible(true);
		
		
		this.setSize(400, 321);
		this.setLayout(null);
		
	}
	/**
	 *  choose course Implementation
	 */
	private JComboBox getCourseBox() {
		if (courseBox == null) {
			courseBox = new JComboBox();
			courseBox.setBounds(new Rectangle(211, 79, 95, 25));  
			SQL s = new SQL (null,23);
			s.accept();
			lcourse = (ArrayList)s.getMsg();
			int size = lcourse.size();
			String cID;
				for (int i=0; i<size ; i++)
				{
				//	cID = lcourse.get(i);
				//	SQL s1 = new SQL (cID,18);
				//	s1.accept();
				//	Object msg = s1.getMsg();
				//	System.out.println("this course : " + i + cID); 
					courseBox.addItem(lcourse.get(i));
				}
				
			courseBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					courseFlg = courseBox.getSelectedIndex();
					System.out.println("this is : " + lcourse.get(courseFlg)); 
				}
			});
		}
		return courseBox;
	}
	/**
	 *  choose class Implementation
	 */
	private JComboBox getClassBox() {
		if (classBox == null) {
			classBox = new JComboBox();
			classBox.setBounds(new Rectangle(211, 122, 95, 25));  
			SQL s2 = new SQL (null,24);
			s2.accept();
			System.out.println("this classsss : "+s2.getMsg().toString());
			lclass = (ArrayList)s2.getMsg();
			int sizeClass = lclass.size();
			String classID;
				for (int j=0; j<sizeClass ; j++)
				{
				//	classID = lclass.get(j);
				//	System.out.println("this class : " + j + classID); 
				//	SQL s3 = new SQL (classID,26);
				//	s3.accept();
				//	Object msg = s3.getMsg();
				//	System.out.println("this class : " + j + classID); 
					classBox.addItem(lclass.get(j));
				}
				
				classBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					System.out.println("itemStateChanged()"); 
					classFlg = classBox.getSelectedIndex();
					System.out.println("this is : " + lclass.get(classFlg)); 
				}
			});
		}
		return classBox;
	}
}
	